from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    """用户模型"""
    phone = models.CharField(max_length=11, unique=True, null=True, blank=True, verbose_name="手机号")
    avatar = models.CharField(max_length=500, blank=True, null=True, verbose_name="头像")
    nickname = models.CharField(max_length=50, blank=True, null=True, verbose_name="昵称")
    gender = models.CharField(
        max_length=10, 
        choices=[('male', '男'), ('female', '女'), ('unknown', '未知')],
        default='unknown',
        verbose_name="性别"
    )
    birthday = models.DateField(null=True, blank=True, verbose_name="生日")
    city = models.CharField(max_length=50, blank=True, null=True, verbose_name="城市")
    openid = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="微信OpenID")
    unionid = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="微信UnionID")
    session_key = models.CharField(max_length=100, blank=True, null=True, verbose_name="微信SessionKey")
    is_vip = models.BooleanField(default=False, verbose_name="是否VIP")
    vip_expire_time = models.DateTimeField(null=True, blank=True, verbose_name="VIP过期时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    # 解决与Django默认User模型的冲突
    groups = models.ManyToManyField(
        'auth.Group',
        verbose_name='用户组',
        blank=True,
        help_text='用户所属的组',
        related_name='custom_user_set',
        related_query_name='custom_user'
    )
    
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        verbose_name='用户权限',
        blank=True,
        help_text='用户的特定权限',
        related_name='custom_user_set',
        related_query_name='custom_user'
    )

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = '用户'

    def __str__(self):
        return self.nickname or self.username


class UserAddress(models.Model):
    """用户地址"""
    user = models.ForeignKey(User, related_name='addresses', on_delete=models.CASCADE, verbose_name="用户")
    name = models.CharField(max_length=50, verbose_name="收货人姓名")
    phone = models.CharField(max_length=11, verbose_name="收货人电话")
    province = models.CharField(max_length=50, verbose_name="省份")
    city = models.CharField(max_length=50, verbose_name="城市")
    district = models.CharField(max_length=50, verbose_name="区县")
    detail = models.CharField(max_length=200, verbose_name="详细地址")
    is_default = models.BooleanField(default=False, verbose_name="是否默认地址")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = 'user_address'
        verbose_name = '用户地址'
        verbose_name_plural = '用户地址'
        ordering = ['-is_default', '-created_at']

    def __str__(self):
        return f"{self.user.nickname} - {self.name}"

    @property
    def full_address(self):
        return f"{self.province}{self.city}{self.district}{self.detail}"